using System;
using Unbound.ValueUnbinders;

namespace Unbound.Unbinders
{
	internal class SimpleUnbinder : ISpecificUnbinder
	{
		readonly ITypeSimplicityEvaluator _evaluator;
		readonly IValueUnbinder _valueUnbinder;

		public SimpleUnbinder(IValueUnbinder valueUnbinder, ITypeSimplicityEvaluator evaluator)
		{
			_valueUnbinder = valueUnbinder;
			_evaluator = evaluator;
		}

		public void Unbind(IUnbindingRequest request)
		{
			var value = _valueUnbinder.UnbindValue(request.Source);
			request.Result.Add(request.Prefix, value);
		}

		public bool AppropriatelyUnbinds(IUnbindingRequest request)
		{
			return IsSimple(request.SourceType) ||
			       SpecificValueUnbinderFactory.HasCustomUnbinderAvailable(request.Source);
		}

		bool IsSimple(Type type)
		{
			return _evaluator.IsSimple(type);
		}
	}
}